#!/usr/bin/env python
#_*_coding:utf-8 _*_
__author__ = 'silence'
import sys,time,datetime
import ATM_operate,file_operate

def operate_menu(login_user):
    while True:
        print u'''\033[31;0m
+++++++++++++++++++++++++++++++
| 1.取现\t\t\t\t4.转账     |
| 2.查询明细\t\t\t5.还款     |
| 3.网上购物\t\t\t0.退出     |
+++++++++++++++++++++++++++++++
\033[0m'''
        user_choice = raw_input(u"请输入数字选择操作选项:").strip()
        if user_choice == '0':
            print u"正在退出系统..."
            time.sleep(1)
            sys.exit()
        elif user_choice =='1':
            #print u"取现"
            encha_status = ATM_operate.enchashment(login_user)
            if encha_status == 0:
                print u"\033[32;0m取现成功\033[0m"
            elif encha_status == -1:
                print u"\033[31;0m输入错误\033[0m"
            elif encha_status == 1:
                print u"\033[31;0m金额超出\033[0m"
            while True:
                print u'''选择是否继续或退出\033[31;0m
+++++++++++++++++++++++++++++++++
|1.返回\t\t2.继续\t\t3.退出  |
+++++++++++++++++++++++++++++++++\033[0m
'''
                choice_encha = raw_input(u"请输入数字选择相应的操作:").strip()
                if choice_encha == "1":
                    break
                elif choice_encha == "2":
                    encha_status = ATM_operate.enchashment(login_user)
                    if encha_status == 0:
                        print u"\033[32;0m取现成功\033[0m"
                    elif encha_status == -1:
                        print u"\033[31;0m输入错误\033[0m"
                    elif encha_status == 1:
                        print u"\033[31;0m金额超出\033[0m"
                elif choice_encha == "3":
                    sys.exit()
                else:
                    print u"输入错误，重新选择"
                    continue
        elif user_choice =='2':
            #print u"查询明细"
            while True:
                print u'''\033[31;1m
+++++++++++++++++++++++++++++++++
|1.查询余额\t\t2.查询明细|
+++++++++++++++++++++++++++++++++\033[0m
'''
                search_choice = raw_input(u"请输入数字编号选择操作(quit退出):").strip()
                if search_choice.isdigit():
                    if search_choice =="1":
                        user_info = file_operate.file_read("user_info.pkl")
                        total_money = user_info[login_user][3]
                        consumption_money = user_info[login_user][4]
                        avilable_money = total_money - consumption_money
                        print u"您当前余额为\033[31;1m%.2f￥\033[0m" %avilable_money
                        break
                    elif search_choice == "2":
                        now_month = datetime.datetime.now().month
                        ATM_operate.show_details(login_user,now_month)
                        break
                elif search_choice == "quit":
                    print u"系统正在退出..."
                    time.sleep(1)
                    sys.exit()
                else:
                    print u"输入错误,",
                    continue
        elif user_choice =='3':
            #print u"网上购物"
            while True:
                shop_status = ATM_operate.shop(login_user)
                if shop_status:
                    break
                else:
                    continue
        elif user_choice =='4':
            #print u"转账"
            transfer_status = ATM_operate.transfer_accounts(login_user)
            if transfer_status ==0:
                print u"\033[32;0m转账成功\033[0m"
            elif transfer_status ==-1:
                print u"\033[31;0m转出用户不存在\033[0m"
            elif transfer_status ==1:
                print u"\033[31;0m转正金额不正确或超出可用金额\033[0m"
            while True:
                print u'''选择是否继续或退出\033[31;0m
+++++++++++++++++++++++++++++++++
|1.返回\t\t2.继续\t\t3.退出  |
+++++++++++++++++++++++++++++++++\033[0m
                '''
                choice_encha = raw_input(u"请输入数字选择相应的操作:").strip()
                if choice_encha == "1":
                    break
                elif choice_encha == "2":
                    transfer_status = ATM_operate.transfer_accounts(login_user)
                    if transfer_status ==0:
                        print u"\033[32;0m转账成功\033[0m"
                    elif transfer_status ==-1:
                        print u"\033[31;0m转出用户不存在\033[0m"
                    elif transfer_status ==1:
                        print u"\033[31;0m转正金额不正确或超出可用金额\033[0m"
                elif choice_encha == "3":
                    sys.exit()
                else:
                    print u"输入错误，重新选择"
                    continue
        elif user_choice =='5':
            print u"\033[31;1m此功能尚未开放...\033[0m"
            continue
        else:
            print u"输入错误,",
            continue

if __name__ == '__main__':
    operate_menu("zhangduanyang")